from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.response import Response
from rest_framework.filters import OrderingFilter

from goods.serializers import SKUSerializer
from goods.models import SKU
# Create your views here.


# GET /categories/(?P<category_id>\d+)/skus?
# class SKUListView(GenericAPIView):
class SKUListView(ListAPIView):
    # queryset = SKU.object.filter(category_id=category_id, is_launched=True)
    serializer_class = SKUSerializer

    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time','price','sales')

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True)

    # def get(self,request,category_id):
    #     """获取分类商品列表信息：
    #         1.根据category_id获取分类下面所有商品信息
    #         2.将商品信息序列化病返回
    #     """
    #     # 1.根据category_id获取分类下面所有商品信息
    #     skus = self.get_queryset()
    #
    #     # 2.将商品信息序列化病返回
    #     serializer = self.get_serializer(skus,many=True)
    #     return Response(serializer.data)




